package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.dao.AqiFeedbackDao;
import com.example.demo.pojo.AqiFeedback;
import com.example.demo.service.IAqiFeedbackService;
import com.example.demo.vos.AfPageRequestDto;
import com.example.demo.vos.PageResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Primary
public class AqiFeedbackServiceImpl implements IAqiFeedbackService {

    @Autowired
    private AqiFeedbackDao aqiFeedbackDao;


    @Override
    public int saveAqiFeedback(AqiFeedback aqiFeedback) {
        return aqiFeedbackDao.insert(aqiFeedback);
    }

    @Override
    public List<AqiFeedback> listAqiFeedbackByTelId(String telId) {
        return aqiFeedbackDao.listAqiFeedbackByTelId(telId);
    }

    @Override
    public PageResponseDto<AqiFeedback> listAqiFeedbackPage(AfPageRequestDto request) {
        PageResponseDto<AqiFeedback> response = new PageResponseDto<>();

        // 获取总行数
        int totalRow = aqiFeedbackDao.getAqiFeedbackCount(request);
        response.setTotalRow(totalRow);

        // 如果总行数为0，直接返回
        if (totalRow == 0) {
            return response;
        }

        // 计算总页数
        int totalPageNum = (totalRow + request.getMaxPageNum() - 1) / request.getMaxPageNum();
        response.setTotalPageNum(totalPageNum);

        // 计算上一页和下一页
        int pageNum = request.getPageNum();
        response.setPreNum(pageNum > 1 ? pageNum - 1 : pageNum);
        response.setNextNum(pageNum < totalPageNum ? pageNum + 1 : pageNum);

        // 计算查询起始位置
        request.setBeginNum((pageNum - 1) * request.getMaxPageNum());
        List<AqiFeedback> list = aqiFeedbackDao.listAqiFeedbackPage(request);

        // 填充返回数据
        response.setPageNum(pageNum);
        response.setMaxPageNum(request.getMaxPageNum());
        response.setList(list);

        return response;
    }

        @Override
        public AqiFeedback getAqiFeedbackById(Integer afId) {
            return aqiFeedbackDao.getAqiFeedbackById(afId);
        }

    @Override
    public int updateAqiFeedbackAssign(AqiFeedback aqiFeedback) {
        UpdateWrapper<AqiFeedback> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("gm_id", aqiFeedback.getGmId());
        updateWrapper.set("assign_date", aqiFeedback.getAssignDate());
        updateWrapper.set("assign_time", aqiFeedback.getAssignTime());
        updateWrapper.set("state", aqiFeedback.getState());
        updateWrapper.eq("af_id", aqiFeedback.getAfId());
        return aqiFeedbackDao.update(null, updateWrapper);
    }

    @Override
    public List<AqiFeedback> listAqiFeedbackByGmId(AqiFeedback aqiFeedback) {
        return aqiFeedbackDao.listAqiFeedbackByGmId(aqiFeedback);
    }

    @Override
    public int updateAqiFeedbackState(AqiFeedback aqiFeedback) {
        UpdateWrapper<AqiFeedback> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("af_id", aqiFeedback.getAfId());
        updateWrapper.set("state", aqiFeedback.getState());
        return aqiFeedbackDao.update(null, updateWrapper);
    }

}
